package vcp.client.GUI.mvc.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;

import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.GUI.factories.StatisticsChartFactory;
import vcp.client.GUI.mvc.view.addons.JCalendarWrapper;
import vcp.client.GUI.mvc.view.custom.CustomCalendarIconButton;
import vcp.client.forms.PeriodicForm;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.client.statistics.StatisticsAbstract;
import vcp.client.utility.Common.UserNotification;
import vcp.client.utility.UtilityHelper;
import vcp.enums.PopUpMessageType;
import vcp.enums.ReportType;

public class UIStatisticsView extends UIAbstractView
{
	/**
	 * 
	 */
	private static final long serialVersionUID = - 8636326112794126307L;

	private JTextField tfStartDate;
	private JTextField tfEndDate;
	private JComboBox <ReportType> cbStatisticsType;
	private JButton btnSearch;
	private JPanel panelGraph;

	private IFormSubmitionListener <PeriodicForm> formSubmitionListener;

	public void setFormSubmitionListener ( IFormSubmitionListener <PeriodicForm> formSubmitionListener )
	{
		this.formSubmitionListener = formSubmitionListener;
	}

	public UIStatisticsView ()
	{
		super();
	}

	// ****************************************************************************************************************************
	// Public methods
	// ****************************************************************************************************************************
	public void loadStatistics ( StatisticsAbstract statistics )
	{
		this.panelGraph.removeAll();
		this.panelGraph.add(StatisticsChartFactory.createStatisticsChart(statistics));
		this.validate();
		this.repaint();
	}

	// ****************************************************************************************************************************
	// Private methods
	// ****************************************************************************************************************************
	/**
	 * @return true when period start & end were filled
	 */
	private boolean hasStartAndEndDate ()
	{
		return UtilityHelper
				.getUtilityHelperInstance()
				.isParamsNotNull(this.tfStartDate.getText(), this.tfEndDate.getText());
	}

	/**
	 * @return the chosen report type
	 */
	private ReportType getSelectedReportType ()
	{
		return (ReportType) this.cbStatisticsType.getSelectedItem();
	}

	// ****************************************************************************************************************************
	// UIAbstractView methods
	// ****************************************************************************************************************************
	@Override
	protected void setupLayout ()
	{
		this.setPreferredSize(new Dimension(908, 704));
		this.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		this.add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));

		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.WEST);

		JLabel label = new JLabel("Start Date:");
		panel_1.add(label);
		label.setFont(new Font("Lucida Grande", Font.PLAIN, 18));

		this.tfStartDate = new JTextField();
		panel_1.add(this.tfStartDate);
		this.tfStartDate.setPreferredSize(new Dimension(125, 28));
		this.tfStartDate.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		this.tfStartDate.setEditable(false);

		CustomCalendarIconButton customCalendarIconButton = new CustomCalendarIconButton(this.tfStartDate);
		panel_1.add(customCalendarIconButton);
		customCalendarIconButton.setPreferredSize(new Dimension(30, 25));

		Component horizontalStrut = Box.createHorizontalStrut(20);
		panel_1.add(horizontalStrut);

		this.tfEndDate = new JTextField();
		panel_1.add(this.tfEndDate);
		this.tfEndDate.setPreferredSize(new Dimension(125, 28));
		this.tfEndDate.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		this.tfEndDate.setEditable(false);

		JLabel label_1 = new JLabel("End Date:");
		panel_1.add(label_1);
		label_1.setFont(new Font("Lucida Grande", Font.PLAIN, 18));

		CustomCalendarIconButton customCalendarIconButton_1 = new CustomCalendarIconButton(this.tfEndDate);
		panel_1.add(customCalendarIconButton_1);
		customCalendarIconButton_1.setPreferredSize(new Dimension(30, 25));

		Component horizontalStrut_1 = Box.createHorizontalStrut(20);
		panel_1.add(horizontalStrut_1);

		this.cbStatisticsType = new JComboBox <>();
		this.cbStatisticsType.setModel(new DefaultComboBoxModel <ReportType>(ReportType.ceoStatisticalReports()));
		panel_1.add(this.cbStatisticsType);
		this.cbStatisticsType.setFont(new Font("Lucida Grande", Font.PLAIN, 15));

		this.btnSearch = new JButton("Search");
		panel.add(this.btnSearch, BorderLayout.EAST);
		this.btnSearch.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		this.btnSearch.setActionCommand("Search");

		this.panelGraph = new JPanel();
		this.add(this.panelGraph, BorderLayout.CENTER);
	}

	@Override
	protected void setupListeners ()
	{
		this.btnSearch.addActionListener(this);
	}

	// ****************************************************************************************************************************
	// ActionListener methods
	// ****************************************************************************************************************************
	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		if ( this.hasStartAndEndDate() )
		{
			DateTimeFormatter formatter = DateTimeFormat.forPattern(JCalendarWrapper.getCalendarDateFormat());
			DateTime startDate = formatter.parseDateTime(this.tfStartDate.getText());
			DateTime endDate = formatter.parseDateTime(this.tfEndDate.getText());
			if ( this.formSubmitionListener != null )
			{
				// fire event using formSubmitionListener delegate
				this.formSubmitionListener.onFormSubmitted(new FormSubmitionEvent <>(this, new PeriodicForm(startDate, endDate, this.getSelectedReportType())));
			}
		}
		else
		{
			// presenting notification to user, start & end need to be
			// filled
			this.controllerDelegate.showPopUpMessage(UserNotification.MUST_FILL_START_AND_END_DATE, PopUpMessageType.INFORMATION);
		}
	}
}
